package com.bbx.taxi.client.Task;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Handler;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.official.passanger.Officail_ComboBuild;
import com.bbx.api.sdk.model.official.passanger.Return.Officail_Combo;
import com.bbx.api.sdk.model.official.passanger.Return.Officail_Vehicle.VehicleList;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.official.passanger.Officail_ComboNet;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;

/** 
 * @Path com.bbx.taxi.client.Task.MyComboTask.java
 * @Author 严旭文
 * @Date 2016-1-26 上午11:32:27
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 * 套现价格
 */
public class MyComboTask{
	private Activity context;
	private MyApplication mApplication;
	public static MyComboTask instance;
	MainAttribute main;
	public interface OnComboListener {
		/**
		 *套现价格获取完成，null则为失败
		 */
		public void onComboFinish(String message);
	}

	static OnComboListener mOnComboListener = null;

	public static void setOnComboListener(OnComboListener l) {
		mOnComboListener = l;
	}
	public static MyComboTask getInstance(Activity context,VehicleList mVehicleList,
			MyApplication mApplication) {
		instance = new MyComboTask(context,mVehicleList, mApplication);
		return instance;
	}
	public MyComboTask(Activity context,final VehicleList mVehicleList, MyApplication mApplication) {
		this.context = context;
		this.mApplication = mApplication;
		main=MainAttribute.getInstance();
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				getCombo(main.getOwner_type(),mVehicleList);
			}
		}, 300);
	
	}
	/**
	 * 获取套现价格
	 */
	public void getCombo(int owner_type,VehicleList mVehicleList) {
     try{
      int type=Integer.parseInt(main.getVtype());
      //如果是出租车
      if(type==Value.taxi_vtype){
    		if (mOnComboListener != null) {
				mOnComboListener.onComboFinish("");
			}
      }else{
    	if(type!=Value.vtype_null){
  		Officail_ComboBuild mOfficail_ComboBuild = new Officail_ComboBuild(context);
  		mOfficail_ComboBuild.type=type;
  		mOfficail_ComboBuild.car_level=Integer.parseInt(main.getLevelCar());
  		mOfficail_ComboBuild.owner_type=owner_type;
  		mOfficail_ComboBuild.car_seats =Integer.parseInt(main.getSeat_num());
  		mOfficail_ComboBuild.tc_id=main.getTc_id();
  		ComboTask mComboTask=new ComboTask(context, mOfficail_ComboBuild);
  		mComboTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    	}
      }
     }catch(Exception e){}
	}
	private class ComboTask extends BaseAsyncTask {

		private Officail_ComboBuild mOfficail_ComboBuild;

		public ComboTask(Activity context, Officail_ComboBuild mOfficail_ComboBuild) {
			super(context);
			this.mOfficail_ComboBuild = mOfficail_ComboBuild;
		}

		@Override
		public void onSuccess(Object object) {
			if (object != null) {
				Officail_Combo mOfficail_Combo=(Officail_Combo)object;
				if (mOnComboListener != null) {
					mOnComboListener.onComboFinish(mOfficail_Combo.getInfo());
				}
			} else {
				ToastUtil.showToast(context, R.string.no_network);
				if (mOnComboListener != null) {
					mOnComboListener.onComboFinish(null);
				}
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			//私有的获取失败自动获取委托车,必须上一次owner_type为私有的，才能自动获取委托的
			if(status==-12&&mOfficail_ComboBuild.owner_type==0){
				//获取委托的，，
//				getCombo(1);
			}else{
				ToastUtil.showToast(context, R.string.no_network);
				if (mOnComboListener != null) {
					mOnComboListener.onComboFinish(null);
				}
			}
	
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mOfficail_ComboBuild).getJson();
			return new Officail_ComboNet(context, json);
		}
	}

}
